-
Notifications
You must be signed in to change notification settings - Fork 208
feat(ai): Support OpenAI transcriptions via @posthog/ai #2498
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(ai): Support OpenAI transcriptions via @posthog/ai #2498
Conversation
|
@Tehnix is attempting to deploy a commit to the PostHog Team on Vercel. A member of the Team first needs to authorize it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2 files reviewed, 3 comments
| const wrappedPromise = parentPromise.then( | ||
| async (result) => { | ||
| if ('text' in result) { | ||
| const latency = (Date.now() - startTime) / 1000 | ||
| await sendEventToPosthog({ | ||
| client: this.phClient, | ||
| ...posthogParams, | ||
| model: String(openAIParams.model ?? ''), | ||
| provider: 'openai', | ||
| input: openAIParams.prompt, | ||
| output: result.text, | ||
| latency, | ||
| baseURL: this.baseURL, | ||
| params: body, | ||
| httpStatus: 200, | ||
| usage: { | ||
| inputTokens: result.usage?.type === 'tokens' ? (result.usage.input_tokens ?? 0) : 0, | ||
| outputTokens: result.usage?.type === 'tokens' ? (result.usage.output_tokens ?? 0) : 0, | ||
| }, | ||
| }) | ||
| return result | ||
| } | ||
| }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: function returns undefined when result doesn't have text property (e.g., 'srt', 'vtt', 'text' response formats)
| const wrappedPromise = parentPromise.then( | |
| async (result) => { | |
| if ('text' in result) { | |
| const latency = (Date.now() - startTime) / 1000 | |
| await sendEventToPosthog({ | |
| client: this.phClient, | |
| ...posthogParams, | |
| model: String(openAIParams.model ?? ''), | |
| provider: 'openai', | |
| input: openAIParams.prompt, | |
| output: result.text, | |
| latency, | |
| baseURL: this.baseURL, | |
| params: body, | |
| httpStatus: 200, | |
| usage: { | |
| inputTokens: result.usage?.type === 'tokens' ? (result.usage.input_tokens ?? 0) : 0, | |
| outputTokens: result.usage?.type === 'tokens' ? (result.usage.output_tokens ?? 0) : 0, | |
| }, | |
| }) | |
| return result | |
| } | |
| }, | |
| const wrappedPromise = parentPromise.then( | |
| async (result) => { | |
| if ('text' in result) { | |
| const latency = (Date.now() - startTime) / 1000 | |
| await sendEventToPosthog({ | |
| client: this.phClient, | |
| ...posthogParams, | |
| model: String(openAIParams.model ?? ''), | |
| provider: 'openai', | |
| input: openAIParams.prompt, | |
| output: result.text, | |
| latency, | |
| baseURL: this.baseURL, | |
| params: body, | |
| httpStatus: 200, | |
| usage: { | |
| inputTokens: result.usage?.type === 'tokens' ? (result.usage.input_tokens ?? 0) : 0, | |
| outputTokens: result.usage?.type === 'tokens' ? (result.usage.output_tokens ?? 0) : 0, | |
| }, | |
| }) | |
| } | |
| return result | |
| }, |
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/ai/src/openai/index.ts
Line: 820:842
Comment:
**logic:** function returns `undefined` when result doesn't have `text` property (e.g., 'srt', 'vtt', 'text' response formats)
```suggestion
const wrappedPromise = parentPromise.then(
async (result) => {
if ('text' in result) {
const latency = (Date.now() - startTime) / 1000
await sendEventToPosthog({
client: this.phClient,
...posthogParams,
model: String(openAIParams.model ?? ''),
provider: 'openai',
input: openAIParams.prompt,
output: result.text,
latency,
baseURL: this.baseURL,
params: body,
httpStatus: 200,
usage: {
inputTokens: result.usage?.type === 'tokens' ? (result.usage.input_tokens ?? 0) : 0,
outputTokens: result.usage?.type === 'tokens' ? (result.usage.output_tokens ?? 0) : 0,
},
})
}
return result
},
```
How can I resolve this? If you propose a fix, please make it concise.| const parentPromise = openAIParams.stream | ||
| ? super.create(openAIParams, options) | ||
| : super.create(openAIParams, options) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: redundant ternary - both branches call the same function
| const parentPromise = openAIParams.stream | |
| ? super.create(openAIParams, options) | |
| : super.create(openAIParams, options) | |
| const parentPromise = super.create(openAIParams, options) |
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/ai/src/openai/index.ts
Line: 742:744
Comment:
**style:** redundant ternary - both branches call the same function
```suggestion
const parentPromise = super.create(openAIParams, options)
```
How can I resolve this? If you propose a fix, please make it concise.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is neccessary for TypeScript to figure out which overloaded super.create function to call, and removing this will result in a typescript error:
Argument of type 'TranscriptionCreateParams' is not assignable to parameter of type 'TranscriptionCreateParamsStreaming'.
Type 'TranscriptionCreateParamsNonStreaming<AudioResponseFormat | undefined>' is not assignable to type 'TranscriptionCreateParamsStreaming'.
Types of property 'stream' are incompatible.
Type 'false | null | undefined' is not assignable to type 'true'.
Type 'undefined' is not assignable to type 'true'.ts(2769)
|
This PR hasn't seen activity in a week! Should it be merged, closed, or further worked on? If you want to keep it open, post a comment or remove the |
|
This is still very much relevant, I'm running a locally patched version of @posthog/ai currently that adds this. |
|
@Tehnix maybe @daibhin or @andrewm4894 could help us |
|
I'm afraid I know very little about the LLM stuff in our SDK but have tagged the @PostHog/team-llm-analytics team who are responsible for this |
|
@Tehnix thanks for this! Looks great - I'll have a play around with it and try test it out locally. |
# Conflicts: # packages/ai/tests/openai.test.ts
- Add language, response_format, and timestamp_granularities to getModelParams - Ensures transcription-specific parameters are captured in $ai_model_parameters - Fixes test failures for transcription language and response_format tests
Wiz Scan Summary
To detect these findings earlier in the dev lifecycle, try using Wiz Code VS Code Extension. |
|
@PostHog/team-llm-analytics tested this and all works great - added some tests too |
Added changeset for the new OpenAI audio transcriptions API support in @posthog/ai package. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
|
the ci errors here seem to be related to fact OP is not a member of the posthog org. have tested this and confirms works as expected - thanks @Tehnix for the contribution!!! 🙌 |
|
@Tehnix thanks again for the contribution here - please email me at [email protected] if you want a voucher for some posthog merch! |
Problem
I've tried to follow the established conventions to enable wrapping the transcriptions API so that it can be used alongside the
@posthog/aisdk.Relevant community thread in the PostHog docs https://posthog.com/questions/support-for-audio-transcriptions
Changes
Release info Sub-libraries affected
Libraries affected
Checklist
If releasing new changes
pnpm changesetto generate a changeset file